import { _decorator, Animation, IPhysics2DContact, BoxCollider2D, CircleCollider2D, Component, Contact2DType, Input, input, RigidBody2D, Vec2, Node, Label, AudioSource, AudioClip } from 'cc';
const { ccclass, property } = _decorator;
import { GameManager, GameStatus, ColliderType } from './GameManager';
import { AudioMgr } from './AudioMgr';

@ccclass('ControlBird')
export class ControlBird extends Component {
  @property({ type: RigidBody2D })
  RigidBody2DComponent: RigidBody2D
  @property({ type: Animation })
  AnimationComponent: Animation
  @property({ tooltip: '旋转速度' })
  RotationSpeed: number = 50
  @property({ type: CircleCollider2D })
  CircleCollider2DComponent: CircleCollider2D
  @property({ type: Node })
  GameEndUINode: Node
  @property({ type: Node })
  GamePlayUINode: Node
  @property({ type: Node })
  PanelNode: Node
  @property({ type: AudioClip })
  AudioClipFile: AudioClip
  start() {
    input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this)
    this.CircleCollider2DComponent.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
    if (GameManager.status !== GameStatus.GamePlay) {
      this.pauseBird()
    }
  }
  onDestroy() {
    input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this)
    this.CircleCollider2DComponent.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
  }
  update(deltaTime: number) {
    if (GameManager.status === GameStatus.GamePlay) {
      this.playBird()
      const RotationAngle = this.RotationSpeed * deltaTime
      this.node.angle = this.node.angle - RotationAngle
      if (this.node.angle < -60) {
        this.node.angle = -60
      }
    }
    else {
      this.pauseBird()
    }
  }
  onMouseUp() {
    if (GameManager.status === GameStatus.GamePlay) {
      this.RigidBody2DComponent.linearVelocity = new Vec2(0, 8)
      this.node.angle = 30
      AudioMgr.inst.playOneShot(this.AudioClipFile)
    }
  }
  pauseBird() {
    this.RigidBody2DComponent.enabled = false
    this.AnimationComponent.enabled = false
  }
  playBird() {
    this.RigidBody2DComponent.enabled = true
    this.AnimationComponent.enabled = true
  }
  onBeginContact(selfCollider: CircleCollider2D, otherCollider: BoxCollider2D, contact: IPhysics2DContact | null) {
    if (otherCollider.tag === ColliderType.Score) {
      GameManager.addScore()
    }
    else {
      GameManager.status = GameStatus.GameEnd
      this.GamePlayUINode.active = false
      if (GameManager.Score <= 10) {
        this.PanelNode.getChildByName('Medal001').active = true
      }
      else if (GameManager.Score <= 20) {
        this.PanelNode.getChildByName('Medal002').active = true
      }
      else if (GameManager.Score <= 30) {
        this.PanelNode.getChildByName('Medal003').active = true
      }
      else {
        this.PanelNode.getChildByName('Medal004').active = true
      }
      this.PanelNode.getChildByName('CurrentScore').getComponent(Label).string = String(GameManager.Score)
      if (localStorage.getItem('HistoryScore') !== null) {
        const HistoryScore = Number(localStorage.getItem('HistoryScore'))
        if (GameManager.Score > HistoryScore) {
          this.PanelNode.getChildByName('BestScore').getComponent(Label).string = String(GameManager.Score)
          this.PanelNode.getChildByName('New').active = true
          localStorage.setItem('HistoryScore', String(GameManager.Score))
        }
        else {
          this.PanelNode.getChildByName('BestScore').getComponent(Label).string = String(HistoryScore)
        }
      }
      else {
        localStorage.setItem('HistoryScore', String(GameManager.Score))
        this.PanelNode.getChildByName('BestScore').getComponent(Label).string = String(GameManager.Score)
        this.PanelNode.getChildByName('New').active = true
      }
      this.GameEndUINode.active = true
    }
  }
}


